﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LEVBatteryPackControl
{
    public class LEVBPCommandProvider
    {
        public enum Cmd_GetInform : byte
        {
            //Cmd for User
            Cmd_GetTemperature_0_1Kel = 0x08,	//0x0b = ntc1, 0x0c = nte2
            Cmd_GetCurrent_mA_HiWord = 0x04,	//0x0b = read
            Cmd_GetVoltage_mV_HiWord = 0x06,	//0x0b = read
            Cmd_GetAvgCurrent_mA_HiWord = 0x07,	//0x0b = read
            Cmd_GetRemainingCapacity_mAh_HiWord = 0x1e,	//0x0b = read
            Cmd_GetFullChargeCapacity_mAh_HiWord = 0x1f,	//0x0b = read

            Cmd_GetCurrent_mA_LoWord = 0x0A,	//0x0b = read
            Cmd_GetVoltage_mV_LoWord = 0x09,	//0x0b = read
            Cmd_GetAvgCurrent_mA_LoWord = 0x0B,	//0x0b = read
            Cmd_GetRemainingCapacity_mAh_LoWord = 0x0F,//0x0b = read
            Cmd_GetFullChargeCapacity_mAh_LoWord = 0x10,	//0x0b = read

            Cmd_GetRelativeStateOfCharge = 0x0D,	//0x0b = read
            Cmd_GetAbsoluteStateOfCharge = 0x0E,	//0x0b = read
            Cmd_GetCycleCountRecord = 0x17,	//0x0b = read

            Cmd_GetSeriesCellVoltage_1Sto7S_mV = 0x24,	//0x0b = read
            Cmd_GetSeriesCellVoltage_7StoLast_mV = 0x25,	//0x0b = read

            Cmd_GetSystemProtectionStatus = 0x49,	//0x0b = read

            //Cmd for Manufacture,
            Cmd_Echo_Test = 0xA0,
            Cmd_GetMcuUID = 0xA1,
            Cmd_GetFirmWareVersion = 0xA2,
            Cmd_GetSerialNumber = 0xA3,	//0x0b = read
            Cmd_GetManufactureDate = 0xA4,		//0x0b = read
            Cmd_GetAllADCData = 0xA5,
            Cmd_GetProjectName = 0xA6,
            Cmd_GetDesignCapacity = 0xA7,
            Cmd_GetBarCodeFor1st12Bytes = 0xA8,
            Cmd_GetBarCodeFor2nd12Bytes = 0xA9,
            Cmd_GetAllBarCode24Bytes = 0xAA,
            Cmd_GetSystemSettingInfor = 0xAB,

        }
        public enum Cmd_SetInform : byte
        {
            Cmd_SetSerialNumber = 0xA3,	//0x0b = read
            Cmd_SetManufactureDate = 0xA4,		//0x0b = read
            Cmd_SetBarCodeFor1st12Bytes = 0xA8,
            Cmd_SetBarCodeFor2nd12Bytes = 0xA9,
            Cmd_SetAllBarCode24Bytes = 0xAA,
            Cmd_SetOneWireTransmitEnableByButton = 0xB0,
            Cmd_SetOneWireTransmitContinues = 0xB1,
            Cmd_SetOneWireTransmitOnce = 0xB2,
            Cmd_DisplayingAllLEDs = 0xB3,		//Test
            Cmd_SetDSGMosFetTurnOnOff = 0xB4,
            Cmd_SetCHGMosFetTurnOnOff = 0xB5,

            Cmd_SetCal_DSG_OP_ADC_OFFSET = 0xD0,
            Cmd_SetCal_CHG_OP_ADC_OFFSET = 0xD1,
            Cmd_SetCal_VBAT_ADC_OFFSET = 0xD2,
            Cmd_SetCal_NTC1_ADC_OFFSET = 0xD3,
            Cmd_SetCal_NTC2_ADC_OFFSET = 0xD4,
            Cmd_SetCal_SOC_ADC_OFFSET = 0xD5,

            Cmd_Read_EepromAddressData = 0xE0,
            Cmd_Write_1byteEepromAddressData = 0xE1,
            Cmd_Write_4bytesEepromAddressData = 0xE4,

            Cmd_ResetSystem = 0xFA,
            Cmd_SetFirmwareUpdatedMode = 0xFC,
        }
        public enum RespondedCode : byte
        {
            Respond_Error_Check_Code = (0xF3),
            Respond_Accept_Check_Code = (0xF0)
        }

        public static byte[] CmdToLEVBP(byte BatteryCmd, byte[] parameters)
        {
            return LEVUtilityLibrary.BatteryPack_UARTPacketFormat.Packing(BatteryCmd, parameters);
        }
        public static byte[] CmdToLEVBP<T>(T BatteryCmd, byte[] parameters) where T : struct, IConvertible
        {

            bool flag = false;
            //string str = "";
            if (!typeof(T).IsEnum) throw new ArgumentException("BatteryCmd Error, T must be an enumerated type of Battery Cmd");
            if (typeof(Cmd_GetInform) == typeof(T) ||
                typeof(Cmd_SetInform) == typeof(T))
            {
                flag = true;
            }
            if (!flag) throw new ArgumentException("BatteryCmd Error, T must be an enumerated type of Battery Cmd");
            byte cmd;
            //Cmd_UART_RS485_Transmit_Data = (0x92),
            //str = Enum.GetName(typeof(T), ToolKitCmd); //= Cmd_UART_RS485_Transmit_Data
            //str = ((byte)Enum.Parse(typeof(T), ToolKitCmd.ToString())).ToString(); // = 0x92
            //str = typeof(T).ToString(); // = B2BToolKitControl.Cmd_ToolKitDataTransmit
            //str = ToolKitCmd.GetType().ToString(); // = B2BToolKitControl.Cmd_ToolKitDataTransmit
            cmd = (byte)Enum.Parse(typeof(T), BatteryCmd.ToString());
            byte[] sendingCmd = LEVUtilityLibrary.BatteryPack_UARTPacketFormat.Packing(cmd, parameters);
            //str = ToolKitCmd.GetType().ToolKitCmd;
            return sendingCmd;
            //{
            //}
            //LEVUtilityLibrary.B2BToolKit_USBPacketFormat.Packing(Enum. 
        }
    }//public class LEVBPCommandProvider
}//namespace LEVBatteryPackControl
